package nju.bishe.weibo.util;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Geocoder;
import android.location.Location;
import android.util.Log;

import weibo4android.WeiboException;
import weibo4android.org.json.JSONException;

import nju.bishe.weibo.TwitterApplication;
import nju.bishe.weibo.adapter.TweetAdapter;
import nju.bishe.weibo.data.Address;
import nju.bishe.weibo.data.Position;

/**
 * 位置管理类
 * @author 吴甲城
 *
 */
public class WeiboLocationManager {
	/**
	 * 获得当前的位置
	 * @return
	 */
	public Position getCurrentPostion(){
		
		Position position = new Position("116.3136", "39.9824");
		try {
			Address address = geoToPosition(116.3136,39.9824);
			position.setCity(address.getCity());
			Log.i("manager", address.getCity()+"wjc");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return position;
	}
	
	/**
	 * 获得附近的位置
	 * @param position
	 * @return
	 * @throws WeiboException
	 * @throws JSONException
	 */
	public List<Position> getNearByPostions(Position position) throws WeiboException, JSONException{
		//调用新浪接口
		double longitude = Double.parseDouble(position.getLongitude());
		double latitude = Double.parseDouble(position.getLatitude());
		
		//用同一个城市名作为查询参数
		List<Position> positions = TwitterApplication.mApi.getNearByPostition(longitude, latitude, position.getCity());
		return positions;
	}
	
	/**
	 * 获得搜索到的位置
	 * @param keyWord
	 * @return
	 * @throws JSONException 
	 * @throws WeiboException 
	 */
	public List<Position> getSearchedPostion(String keyWord,double longitute,double latitude) throws WeiboException, JSONException{
		List<Position> positions = TwitterApplication.mApi.searchPostions(keyWord,latitude,longitute);
		return positions;
	}
	
	/**
	 * 根据坐标得到地理位置详细信息
	 * @param longitude
	 * @param latitude
	 * @return
	 * @throws WeiboException 
	 * @throws JSONException 
	 */
	public Address geoToPosition(double longitude, double latitude) throws WeiboException, JSONException{
		weibo4android.Address waddress=TwitterApplication.mApi.getGeoAdress(latitude, longitude);
		
		return  new Address(waddress);
	}
	
	public void initPosition(){
		Position p=new Position("118.773097","32.060288");
		savePosition(p);
	}
	
	public Position getPosition(){
		SharedPreferences pref = TwitterApplication.mPref;
		String savedLongitude=pref.getString(Preferences.GPS_LONGITUDE, "118.773097");
		String savedLatitude=pref.getString(Preferences.GPS_LATITUDE, "32.060288");
		Position p=new Position(savedLongitude,savedLatitude);
		return p;
	}

	public void savePosition(Position p){
		String latitude=p.getLongitude();
		String longitude=p.getLatitude();
		SharedPreferences.Editor editor = TwitterApplication.mPref
		.edit();
		editor.putString(Preferences.GPS_LONGITUDE, longitude);
		editor.putString(Preferences.GPS_LATITUDE, latitude);
		editor.commit();
	}

	public void setGPSStatus(boolean turner){
		SharedPreferences.Editor editor = TwitterApplication.mPref
		.edit();
		editor.putBoolean(Preferences.GPS_TURNER, turner);
		editor.commit();
	}

	public boolean getGPSStatus(){
		SharedPreferences pref = TwitterApplication.mPref;
		boolean turner= pref.getBoolean(Preferences.GPS_TURNER,false);
		return turner;
	}
	
	/**
	 * 查询酒店位置
	 * @param longitute
	 * @param latitude
	 * @return
	 * @throws JSONException 
	 * @throws WeiboException 
	 */
	public List<Position> getDrinkeryLocations(double longitute,double latitude) throws WeiboException, JSONException{
		return getSearchedPostion("酒店", longitute, latitude);
	}
	
	/**
	 * 根据经度和纬度返回具体的地址信息
	 * @param longitude
	 * @param latitude
	 * @return
	 * @throws IOException 
	 */
	public String getAddressByLatlong(Context context, double longitude,double latitude) throws IOException{
		  Geocoder ge = new Geocoder(context,Locale.CHINA);
		  List<android.location.Address> addrList = ge.getFromLocation(latitude, longitude, 1);
		  android.location.Address address = addrList.get(0);
		  String addressMessage = "";
		  for(int i=0;i<address.getMaxAddressLineIndex()-1;i++){
			  addressMessage+=address.getAddressLine(i);
		  }
		  return addressMessage;
	}
}
